package com.ruoyi.web.controller.wx;


import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.service.SysLoginService;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.web.domain.wx.WxLoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@RequestMapping("/wx/wxLogin")
public class WxLoginController extends BaseController {

    @Autowired
    private SysLoginService loginService;

    @Autowired
    private SysUserMapper sysUserMapper;

    @PostMapping("/passwordLogin")
    public AjaxResult passwordLogin(@RequestBody WxLoginVo wxLoginVo) {

        SysUser sysUser = new SysUser();
        sysUser.setPhonenumber(wxLoginVo.getPhone());
        sysUser.setStatus("0");
        sysUser.setDelFlag("0");
        sysUser.setIsUsed(1);
        List<SysUser> sysUsers = sysUserMapper.selectUserList(sysUser);


        if(!StringUtils.isEmpty(sysUsers) && sysUsers.size() ==1){
            // 生成令牌
            String token = loginService.login(sysUsers.get(0).getUserName(), wxLoginVo.getPassword(), wxLoginVo.getCode(),
                    wxLoginVo.getUuid(), "app");
            return success().put(Constants.TOKEN, token).put("user",sysUsers.get(0));
        }else{
            return error("用户不存在！");
        }



    }

}

